﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLBV.Models;
using System.Data;
using System.Web.UI.WebControls;

namespace QLBV.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/
        QLBVContext db = new QLBVContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Themmenu() 
        {
            //var abc = new SelectList(from b in db.Menus where b.)
            int vitri = int.Parse(Request.QueryString["vitri"].ToString());
            IEnumerable<SelectListItem> IElist = new SelectList(db.Menus.Where(x => x.Parent_ID == 0 && x.Location==vitri), "ID", "Name");
            var Ilist = IElist.ToList();
            Ilist.Insert(0, (new SelectListItem { Text = "Mặc định", Value = "0" }));
            ViewBag.List = Ilist;
            return View();
        }
        [HttpPost]
        public ActionResult Themmenu(Menus menus)
        {
            if (ModelState.IsValid)
            {
                db.Menus.Add(menus);
                db.SaveChanges();
                return RedirectToAction("Menu");

            }
            //IEnumerable<SelectListItem> IElist = new SelectList(db.Menus.Where(x => x.Parent_ID == 0), "ID", "Name", menus.ID);
            //var Ilist = IElist.ToList();
            //Ilist.Insert(0, (new SelectListItem { Text = "Mặc định", Value = "0" }));    
            //ViewBag.List = Ilist;
            return View(menus);
        }

        public ActionResult Menu()
        {
            IList<Menus> menu;
            using (db)
            {
                menu = ((from b in db.Menus select b)).ToList();


            }
            return View(menu);
        }
        public ActionResult Suamenu(int id)
        {
            Menus menus = db.Menus.Find(id);
            IEnumerable<SelectListItem> IElist = new SelectList(db.Menus.Where(x => x.Parent_ID == 0 && x.ID != id), "ID", "Name", menus.ID);
            var Ilist = IElist.ToList();
            Ilist.Insert(0, (new SelectListItem { Text = "Mặc định", Value = "0" }));
            ViewBag.List = Ilist;
            return View(menus);
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        public ActionResult Suamenu(Menus menus)
        {
            if (ModelState.IsValid)
            {
                db.Entry(menus).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Menu");
            }

            //IEnumerable<SelectListItem> IElist = new SelectList(db.Menus.Where(x => x.Parent_ID == 0), "ID", "Name");
            //var Ilist = IElist.ToList();
            //Ilist.Insert(0, (new SelectListItem { Text = "Mặc định", Value = "0" }));
            //ViewBag.List = Ilist;

            return View(menus);
        }

        public ActionResult Xoa(int id)
        {
            Menus menus = db.Menus.Find(id);
            db.Menus.Remove(menus);
            db.SaveChanges();
            return RedirectToAction("Menu");
        }

        //
        // POST: /Menu/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult Xacnhanxoa(int id)
        //{
        //    Menus menus = db.Menus.Find(id);
        //    db.Menus.Remove(menus);
        //    db.SaveChanges();
        //    return RedirectToAction("Menu");
        //}
        //public ActionResult thu()
        //{
        //    int a = int.Parse(Request.QueryString["vitri"].ToString());
        //    int b = int.Parse(UrlParameter.Optional.ToString());
        //    IEnumerable<SelectListItem> IElist = new SelectList(db.Menus.Where(x => x.Parent_ID == 0 && x.Location ==b), "ID", "Name");
        //    var Ilist = IElist.ToList();
        //    Ilist.Insert(0, (new SelectListItem { Text = "Mặc định", Value = "0" }));
        //    ViewBag.List = Ilist;
        //    return View();
        //}
    }

}
